﻿/**********************************************
系统:Ebada物流企业ERP
模块:示例
作者:Rabbit
创建时间:2011-2-22
最后一次修改:2011-2-27
***********************************************/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Ebada.Client.Platform;
using Ebada.Platform.Model;
using DevExpress.XtraGrid.Columns;
using System.Reflection;
using Ebada.Client;
using DevExpress.XtraGrid.Views.Base;
using Ebada.PlatformLog.Model;
using DevExpress.XtraBars;
using System.Threading;
using Ebada.Core;

namespace sample1 {
    /// <summary>
    /// GridView数据操作示例
    /// </summary>
    public partial class UCMillions : UserControl {
        private GridViewOperation<mLog_Operate> gridViewOperation;

        public event SendDataEventHandler<mLog_Operate> FocusedRowChanged;
        private string parentID;
        public UCMillions() {
            InitializeComponent();
            initImageList();
            gridViewOperation = new GridViewOperation<mLog_Operate>(gridControl1, gridView1, barManager1);
            gridViewOperation.CreatingObjectEvent +=gridViewOperation_CreatingObjectEvent;
            gridView1.FocusedRowChanged += new DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventHandler(gridView1_FocusedRowChanged);
        }
        private void initImageList() {
            ImageList imagelist = new ImageList();
            imagelist.ImageStream = (Ebada.Client.Resource.UCGridToolbar.UCGridToolbarImageList);
            barManager1.Images = imagelist;
        }
        void gridView1_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e) {
            if (FocusedRowChanged != null)
                FocusedRowChanged(gridView1, gridView1.GetFocusedRow() as mLog_Operate);
        }
        /// <summary>
        /// 封装了数据操作的对象
        /// </summary>
        public GridViewOperation<mLog_Operate> GridViewOperation {
            get { return gridViewOperation; }
            set { gridViewOperation = value; }
        }
        /// <summary>
        /// 新建对象设置Key值
        /// </summary>
        /// <param name="newobj"></param>
        void gridViewOperation_CreatingObjectEvent(mLog_Operate newobj) {
            newobj.EntryID = newobj.CreateID();
        }
        /// <summary>
        /// 父表ID
        /// </summary>
        public string ParentID {
            get { return parentID; }
            set {
                parentID = value;
            }
        }
        #region 插入数据
        /// <summary>
        /// 插入数据
        /// </summary>
        /// 
        void add10000() {
            if (this.InvokeRequired) {
                this.Invoke(new MethodInvoker(add10000));
            } else {
                //List<mLog_Operate> list = new List<mLog_Operate>();
                addcount = 0;
                for (int i = 0; i < 20; i++) {
                    //btProgress.Caption = (i + 1) * 100 + "/" + 10000;
                    //btProgress.EditValue = i;
                    //this.Refresh();
                    //list.Clear();
                    //IEnumerator<mLog_Operate> ie = getObjects100();
                    //while(ie.MoveNext()) {

                    //    list.Add(ie.Current);
                    //}
                    //MainHelper.GetSqlMap<mLog_Operate>().ExecuteTransationUpdate(list, null, null);                    
                    this.BeginInvoke(new MethodInvoker(insertRows));
                }
            }
        }
        int addrows = 500;
        int addcount = 0;
        void insertRows() {
            List<mLog_Operate> list = new List<mLog_Operate>();
            IEnumerator<mLog_Operate> ie = getObjects100();
            while (ie.MoveNext()) {

                list.Add(ie.Current);
            }
            MainHelper.GetSqlMap<mLog_Operate>().ExecuteTransationUpdate(list, null, null);
            list.Clear();
            addcount += addrows;
            btProgress.Caption = addcount + "/" + 10000;
            btProgress.EditValue = addcount/100;
            this.Refresh();
        }
        IEnumerator<mLog_Operate> getObjects100() {
            
            Random random = new Random();
            for (int j = 0; j < addrows; j++) {
                mLog_Operate obj = new mLog_Operate();
                obj.EntryID = Guid.NewGuid().ToString(); obj.LoginName = "" + random.Next(100000, 1111111);
                yield return obj;
            }            
        }

        #endregion
        bool stop = false;

        #region 读取数据
        int nrows = 500;//每次下载行数
        /// <summary>
        /// 读取数据10000
        /// </summary>
        void read10000() {
            if (this.InvokeRequired) {
                this.Invoke(new MethodInvoker(read10000));
            } else {
                List<mLog_Operate> list = new List<mLog_Operate>();
                int ren = 0; stop = false;
                ncount = 0; gridViewOperation.BindingList.Clear();
                for (int i = 1; i <=20; i++) {
                    if (stop) break;
                    DataTable dt = MainHelper.GetSqlMap<mLog_Operate>().GetDataTable("SelectmLog_OperateList", "order by EntryID", nrows, i, out ren);
                    this.BeginInvoke(new EventHandlerT<DataTable>(initrows), dt);
                    btProgress.Caption = "下载记录行数:" + i*nrows;
                    btProgress.EditValue = i*nrows/100;
                    btProgress.Refresh();
                }
            }
        }
        int ncount = 0;
        void initrows(DataTable dt) {
            if (this.InvokeRequired) {
                this.Invoke(new EventHandlerT<DataTable>(initrows), dt);
            } else {
                if (stop) return;
                gridView1.BeginDataUpdate();
                IEnumerator<mLog_Operate> ie= getobject(dt);
                while(ie.MoveNext()){
                    if (stop) break;
                    gridViewOperation.BindingList.Add(ie.Current);
                }
                gridView1.EndDataUpdate();
                ncount += nrows;
                btProgress.Caption = "显示记录数：" + ncount;
                btProgress.EditValue = ncount / 100;
                btProgress.Refresh();
                gridControl1.Refresh();
            }
        }
        IEnumerator<mLog_Operate> getobject(DataTable dt) {
            foreach (DataRow row in dt.Rows) {
                yield return ConvertHelper.RowToObject<mLog_Operate>(row);
            }
        }
        #endregion

        private void barButtonItem2_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e) {
            MainHelper.GetSqlMap<mLog_Operate>().Update("truncatemLog_Operate", null);//清空表数据
            gridViewOperation.BindingList.Clear();
        }
        
        private void barButtonItem1_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e) {
            this.BeginInvoke(new MethodInvoker(add10000));
        }

        private void barButtonItem3_ItemClick(object sender, ItemClickEventArgs e) {
           this.BeginInvoke(new MethodInvoker(read10000));
        }

        private void gridControl1_EmbeddedNavigator_ButtonClick(object sender, NavigatorButtonClickEventArgs e) {
            if (e.Button.Tag!=null && e.Button.Tag.ToString() == "stop") {
                stop = true;
            }
        }
    }
}
